package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.feigns.OrderApi;
import com.qf.pojo.R;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//@RestController
//@RequestMapping("/user")
//@DefaultProperties(defaultFallback = "defFallback")
public class UserController5 {

    @Autowired
    private OrderApi orderApi;

    /*
        类级别的降级方法
            返回值必须和被保护资源一致
            参数列表为空（可以有一个异常参数）
     */
    public R defFallback(Throwable err){
        err.printStackTrace();

        return R.error().putMsg("默认降级....");
    }

    @GetMapping("/{id}")
    @HystrixCommand
    public R findOrders(@PathVariable("id") Integer id) throws InterruptedException {
        if (id == 1) {
            int i = 1 / 0;
        }

        if (id == 2){
            Thread.sleep(3500);
        }

        if (id == 3){
            Thread.sleep(1000);
        }

        //发起远程访问
        R r = orderApi.findByUid(id);

        return r;
    }

}
